import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0, // 初始计数值
  },
  mutations: {
    increment(state) {
      state.count++; // 增加计数值
    },
    decrement(state) {
      state.count--; // 减少计数值
    },
  },
  actions: {
    incrementAsync({ commit }) {
      setTimeout(() => {
        commit('increment'); // 异步增加计数值
      }, 1000);
    },
    decrementAsync({ commit }) {
      setTimeout(() => {
        commit('decrement'); // 异步减少计数值
      }, 1000);
    },
  },
  getters: {
    getCount: state => {
      return state.count; // 获取当前计数值
    },
  },
})
